package com.free.chat.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.solon.service.impl.ServiceImpl;
import com.free.chat.common.enums.FriendRelationEnum;
import com.free.chat.dao.UserRelationDao;
import com.free.chat.entity.ApiResult;
import com.free.chat.entity.dto.UserFriendDTO;
import com.free.chat.entity.model.UserRelation;
import com.free.chat.entity.model.Users;
import com.free.chat.exception.ApiException;
import com.free.chat.service.UserRelationService;
import com.free.chat.service.UsersService;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.annotation.Inject;
import org.noear.solon.aspect.annotation.Service;
import org.noear.solon.core.handle.Context;

import java.util.Date;
import java.util.Objects;

@Service
public class UserRelationServiceImpl extends ServiceImpl<UserRelationDao, UserRelation> implements UserRelationService {

    @Db
    private UserRelationDao userRelationDao;
    @Inject
    private UsersService usersService;

    @Override
    public ApiResult saveFriend(UserFriendDTO userFriendDTO) {
        Users loginUser = (Users) Context.current().session("loginUser");
        if (loginUser == null) return ApiResult.unAuthorization();

        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getId, userFriendDTO.getUserId())
                .or()
                .eq(Users::getUsername, userFriendDTO.getUsername())
                .or()
                .eq(Users::getNickname, userFriendDTO.getNickname());
        Users friendUser = usersService.getOne(queryWrapper);
        if (friendUser == null) {
            return ApiResult.error("未查询到此人");
        }

        if (Objects.equals(loginUser.getId(), friendUser.getId())) {
            return ApiResult.error("请勿添加自己");
        }

        UserRelation userRelation = new UserRelation();
        userRelation.setId(IdUtil.getSnowflakeNextId());
        userRelation.setUserId(loginUser.getId());
        userRelation.setFriendId(friendUser.getId());
        userRelation.setRelation(FriendRelationEnum.NORMAL.type);
        userRelation.setCreateTime(new Date());
        userRelationDao.insert(userRelation);
        return ApiResult.success();
    }

    @Override
    public ApiResult deleteFriend(UserFriendDTO userFriendDTO) {
        try {
            Long loginUserId = (Long) Context.current().session("loginUserId");
            if (loginUserId == null) return ApiResult.unAuthorization();

            LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Users::getId, userFriendDTO.getUserId())
                    .or()
                    .eq(Users::getUsername, userFriendDTO.getUsername())
                    .or()
                    .eq(Users::getNickname, userFriendDTO.getNickname());
            Users friendUser = usersService.getOne(queryWrapper);
            if (friendUser == null) {
                return ApiResult.success();
            }

            LambdaQueryWrapper<UserRelation> u1 = new LambdaQueryWrapper<>();
            u1.eq(UserRelation::getUserId, loginUserId)
                    .eq(UserRelation::getFriendId, friendUser.getId());
            UserRelation userRelation = new UserRelation();
            userRelation.setRelation(FriendRelationEnum.DELETED.type);
            update(userRelation, u1);
            return ApiResult.success();
        } catch (Exception e) {
            throw new ApiException(e.getMessage());
        }
    }
}
